﻿using System;
using System.AutoUpdate.Core.Operations;
using System.AutoUpdate.Library;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace System.AutoUpdate.Operations
{
    /// <summary>
    /// Operation cleans any files left over from updating
    /// </summary>
    public class CleanOperation : BasicOperation
    {
        public override bool StartOperation (UpdateInfo info)
        {
            if (info == null)
            {
                return false;
            }

            Info = info;

            //
            // Get directory that had updates
            //

            string[] files = Directory.GetFiles (info.TemporaryDirectory);

            foreach (string file in files)
            {
                //
                // Only deleting application package files
                //

                if (Path.GetExtension (file) == ".aup")
                {
                    File.Delete (file);
                }
            }

            //
            // Clean Bootstrap application
            //

            if (File.Exists (Info.ApplicationRoot + "UpdateBootstrap.exe"))
            {
                File.Delete (Info.ApplicationRoot + "UpdateBootstrap.exe");
            }

            if (File.Exists (Info.TemporaryDirectory + "UpdateBootstrap.exe"))
            {
                File.Delete (Info.TemporaryDirectory + "UpdateBootstrap.exe");
            }

            return true;
        }
    }
}
